package b2.widget.store
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	import b2.widget.warehouse.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class PurchaseDialog extends Sprite
	{
    	private var type:int;
    	//卖出坦克时候起作用
    	private var tankId:int;
    	private var objectIndex:int;
    	private var basicLevel:int;
    	private var strengthenLevel:int;
    	private var numSel:NumberSelecter;
    	private var price:int;

    	//type = 0 买, type = 1 卖
		public function PurchaseDialog(tankId:int, objectIndex:int, basicLevel:int, strengthenLevel:int, type:int)
		{
			super();
			this.type = type;
			this.tankId = tankId;
			this.objectIndex = objectIndex;
			this.basicLevel = basicLevel;
			this.strengthenLevel = strengthenLevel;
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
			//透明背景
			var bgr:Sprite = new Sprite();
			var gfx:Graphics = bgr.graphics;
			gfx.beginFill(0x000000, 0.01);
			gfx.drawRect(0, 0, BFont.stageWidth, BFont.stageHeight);
			gfx.endFill();
			addChild(bgr);
			//背景
			var PurchaseDialog:Class = ResourceHelper.getInstance().getClassByName("MessageBox0");
			var dialogSprite:Sprite = new PurchaseDialog();
			dialogSprite.x = (BFont.stageWidth - dialogSprite.width) / 2;
			dialogSprite.y = 140;
			addChild(dialogSprite);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			if (type == 0)
				titleText.text = "购买";
			else
				titleText.text = "售出";
			titleText.x = (dialogSprite.width - titleText.width) / 2;
			titleText.y = 15;
			dialogSprite.addChild(titleText);
			//物品图标
			var ClassImage:Class = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage);
			var imageSprite:Sprite = new ClassImage();
			//齐天大圣
			if (objectIndex == 10015)
			{
				imageSprite.width = 192;
				imageSprite.height = 215;
			}
			//死亡骑士
			else if (objectIndex == 10020)
			{
				imageSprite.width = 168;
				imageSprite.height = 168;
			}
			else
			{
				imageSprite.width = 140;
				imageSprite.height = 140;
			}
			imageSprite.x = 40;
			imageSprite.y = 70;
			dialogSprite.addChild(imageSprite);
			
			//加载名字
			var name:TextField = new TextField();
			name.selectable = false;
			name.autoSize = TextFieldAutoSize.CENTER;
			name.defaultTextFormat = new TextFormat("微软雅黑", 20, 0x000000, true);
			//装备
			if (objectConfig.type == 1)
			{
				var equipConfig:EquipConfig = objectConfig as EquipConfig;
				name.text = Config.basicConfig.equipSuitName[equipConfig.suitIndex] + "的";
			}
			name.appendText(objectConfig.name);
			name.x = 110 - name.width / 2;
			name.y = 215;
			dialogSprite.addChild(name);
			
			//加载价格图标
			var PriceImage:Class;
			//购买
			if (type == 0)
			{
				if (objectConfig.priceGas > 0)
				{
					PriceImage = ResourceHelper.getInstance().getClassByName("PriceGas");
					price = objectConfig.priceGas;
				}
				else if (objectConfig.priceHonour > 0)
				{
					PriceImage = ResourceHelper.getInstance().getClassByName("Honour");
					price = objectConfig.priceHonour;
				}
				else 
				{
					PriceImage = ResourceHelper.getInstance().getClassByName("PriceOil");
					price = objectConfig.priceOil;
				}
			}
			//卖出
			else
			{
				PriceImage = ResourceHelper.getInstance().getClassByName("PriceOil");
				if (objectConfig.priceGas > 0)
				{
					price = objectConfig.priceGas * 10;
				}
				else
				{
					price = objectConfig.priceOil / 5;
				}
				price = price * (basicLevel + 1) + 10 * (strengthenLevel + 1);
			}
			//加载价格图标和价格
			var priceContainer:Sprite = new Sprite();
			var priceImage:Sprite = new PriceImage();
			priceImage.width = 25;
			priceImage.height = 25;
			priceContainer.addChild(priceImage);
			//加载价格
			var priceText:TextField = new TextField();
			priceText.selectable = false;
			priceText.autoSize = TextFieldAutoSize.LEFT;
			priceText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			priceText.text = price.toString();
			priceText.x = priceImage.x + priceImage.width + 10;
			priceText.y = priceImage.y;
			priceContainer.addChild(priceText);
			priceContainer.x = 320 - priceContainer.width / 2;
			priceContainer.y = 75;
			dialogSprite.addChild(priceContainer);			
			//加载具体描述
			var description:TextField = new TextField();
			description.multiline = true;
			description.selectable = false;
			description.autoSize = TextFieldAutoSize.LEFT;
			description.width = 200;
			description.wordWrap = true;
			description.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000);
			var descrip:String = objectConfig.description;
			//装备
			if (objectConfig.type == 1)
			{
				descrip = equipConfig.getDescription(0);
			}
			description.htmlText = descrip;
			description.x = 210;
			description.y = 140;
			dialogSprite.addChild(description);
			
			//加载数量选择器
			var LeftArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp");
			var RightArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown");
			if (type == 0)
			{
				//坦克
				if (objectConfig.type == 0)
					numSel = new NumberSelecter(LeftArrowButton, RightArrowButton, 1, 1, 5);
				else
					numSel = new NumberSelecter(LeftArrowButton, RightArrowButton);
			}
			else
			{
				var me:PlayerInfo = PlayerInfoManager.getInstance().me;
				//坦克
				if (objectConfig.type == 0)
				{
					numSel = new NumberSelecter(LeftArrowButton, RightArrowButton, 1, 1, 1);
				}
				else
				{
					var objectNum:int = me.getObjNum(objectIndex, basicLevel, strengthenLevel);
					numSel = new NumberSelecter(LeftArrowButton, RightArrowButton, objectNum, 1, objectNum);
				}
			}
			dialogSprite.addChild(numSel);
			numSel.x = 280;
			numSel.y = 110;
			
			var gap:int = -60; 
			//确定
			var ButtonBgr1:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			var buttonOK:BasicButton;
			if (type == 0)
			{
				buttonOK = new BasicButton(ButtonBgr1, "购买");
			}
			else if (type == 1)
			{
				buttonOK = new BasicButton(ButtonBgr1, "出售");
			}
			buttonOK.x = 225;
			buttonOK.y = 230;
			dialogSprite.addChild(buttonOK);
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKMouseClick);
			
			//取消
			var buttonCancel:BasicButton = new BasicButton(ButtonBgr1, "取消");
			buttonCancel.x = 325;
			buttonCancel.y = 230;
			dialogSprite.addChild(buttonCancel);
			buttonCancel.addEventListener(MouseEvent.CLICK, onButtonCancelMouseClick);
			
		}
		
		private function onButtonCancelMouseClick(event:MouseEvent):void
        {
        	this.parent.removeChild(this);
        }
        
		private function onButtonOKMouseClick(event:MouseEvent):void
        {
			var objectConfig:CommonObjConfig = Config.objectConfigs[objectIndex];
    		//购买
        	if (type == 0)
        	{
        		ServerHelper.getInstance().purchaseObject(objectConfig.index, numSel.value, objectConfig.priceOil, objectConfig.priceGas, objectConfig.priceHonour);
				ServerHelper.getInstance().addEventListener(ServerEvent.PURCHASE, onPurchaseObjectComplete);
        	}
    		//卖出
        	else if (type == 1)
        	{
        		ServerHelper.getInstance().sellObject(tankId, objectIndex, basicLevel, strengthenLevel, numSel.value, price);
				ServerHelper.getInstance().addEventListener(ServerEvent.SELL, onSellObjectComplete);
        	}
        }
        
        private function onSellObjectComplete(event:ServerEvent):void
        {
        	var popMessage:PopMessage;
        	if (event.result == "success")
        	{
        		popMessage = new PopMessage("出售成功！", 0x00FF00);
        	}
        	else if (event.result == "not enough object")
        	{
        		popMessage = new PopMessage("对不起，您没有足够的物品！", 0xFF0000);
        	}
        	WidgetManager.getInstance().popOut([popMessage], false, true);
        	Warehouse.getInstance().refresh();
        	ServerHelper.getInstance().removeEventListener(ServerEvent.SELL, onSellObjectComplete);
        	close();
        }
        
        private function onPurchaseObjectComplete(event:ServerEvent):void
        {
        	var popMessage:PopMessage;
        	if (event.result == "success")
        	{
        		popMessage = new PopMessage("购买成功，请到仓库中查看！", 0x00FF00);
        	}
        	else if (event.result == "not enough oil")
        	{
        		popMessage = new PopMessage("没有足够的石油用于购买！", 0xFF0000);
        	}
        	else if (event.result == "not enough gas")
        	{
        		popMessage = new PopMessage("没有足够的天然气用于购买！", 0xFF0000);
        	}
        	else if (event.result == "not enough honour")
        	{
        		popMessage = new PopMessage("没有足够的荣誉勋章用于购买！", 0xFF0000);
        	}
        	WidgetManager.getInstance().popOut([popMessage], false, true);
        	Warehouse.getInstance().refresh();
        	ServerHelper.getInstance().removeEventListener(ServerEvent.PURCHASE, onPurchaseObjectComplete);
        	close();
        }
        
        public function close():void
        {
        	if (this.parent != null)
        		this.parent.removeChild(this);
        }
	}
}